package com.ujoku.service.impl;

import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.util.StringUtils;
import com.ujoku.dao.CartDao;
import com.ujoku.dao.OrderDao;
import com.ujoku.domain.*;
import com.ujoku.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andy.Lian on 2014/11/2.
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService<Order> {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private MyListItemService myListItemService;

    @Override
    protected BaseDao<Order> getBaseDao() {
        return orderDao;
    }

    @Override
    public Order selectById(String orderId, int memberId){

        Map<String,Object> query = new HashMap<>();
        query.put("memberId", memberId);
        query.put("id", orderId);
        Order order = super.selectOne(query);

        if(order == null)
            throw new ResourceNotFoundException("can not found order");

        List<OrderItem> orderItemList = orderItemService.selectByOrderId(orderId);

        order.setOrderItemList(orderItemList);

        return order;
    }

    @Override
    public int selectOrderStatusById(String orderId, int memberId){
        Map<String,Object> query = new HashMap<>();
        query.put("memberId", memberId);
        query.put("id", orderId);
        Order order = super.selectOne(query);

        if(order == null)
            throw new ResourceNotFoundException("can not found order");

        return order.getStatus();
    }

    @Override
    public Order selectById(String orderId){

        Order order = super.selectById(orderId);

        if(order == null)
            throw new ResourceNotFoundException("can not found order");

        List<OrderItem> orderItemList = orderItemService.selectByOrderId(orderId);

        order.setOrderItemList(orderItemList);

        return order;
    }

    @Override
    public List<Order> selectList(Map<String,Object> query) {

        List<Order> orderList = super.selectList(query);
        for(Order order : orderList){
            List<OrderItem> orderItemList = orderItemService.selectByOrderId(order.getId());

            order.setOrderItemList(orderItemList);
        }

        return orderList;
    }


    //计算库存和销量
    @Override
    public void setGoodsSaleCount(Order order) {
        for(OrderItem orderItem : order.getOrderItemList()){
            //销售量
            Goods goods = goodsService.selectById(orderItem.getGoodsId());
            if(goods != null)
            {
                goods.setSalesCount(goods.getSalesCount() + orderItem.getQty());
                goodsService.update(goods);
            }
            //库存处理
            GoodsSpec goodsSpec = goodsSpecService.selectGoodsSpecById(orderItem.getGoodsId());
            if(goodsSpec != null){
                goodsSpec.setStock(goodsSpec.getStock() - orderItem.getQty());
                goodsSpecService.update(goodsSpec);
            }
        }
    }

    //回滚库存和销量
    @Override
    public void rollBackGoodsSaleCount(Order order) {
        for(OrderItem orderItem : order.getOrderItemList()){
            //销售量
            Goods goods = goodsService.selectById(orderItem.getGoodsId());
            if(goods != null)
            {
                goods.setSalesCount(goods.getSalesCount() - orderItem.getQty());
                goodsService.update(goods);
            }
            //库存处理
            GoodsSpec goodsSpec = goodsSpecService.selectGoodsSpecById(orderItem.getGoodsId());
            if(goodsSpec != null){
                goodsSpec.setStock(goodsSpec.getStock() + orderItem.getQty());
                goodsSpecService.update(goodsSpec);
            }
        }
    }

    //计算私人酒窖库存和销量
    @Override
    public void setMyListItemSaleCount(Order order) {
        for(OrderItem orderItem : order.getOrderItemList()) {
            if(!StringUtils.hasText(orderItem.getMyListItemId())){
                continue;
            }

            MyListItem myListItem = (MyListItem) myListItemService.selectById(orderItem.getMyListItemId());
            if(myListItem != null){
                myListItem.setSaleCount(myListItem.getSaleCount() + orderItem.getQty());
                //库存处理
                myListItem.setQty(myListItem.getQty() - orderItem.getQty());
                myListItemService.update(myListItem);
            }
        }
    }

    //回滚私人酒窖库存和销量
    @Override
    public void rollBackMyListItemSaleCount(Order order) {
        for(OrderItem orderItem : order.getOrderItemList()) {
            if(!StringUtils.hasText(orderItem.getMyListItemId())){
                continue;
            }

            MyListItem myListItem = (MyListItem) myListItemService.selectById(orderItem.getMyListItemId());
            if(myListItem != null){
                myListItem.setSaleCount(myListItem.getSaleCount() - orderItem.getQty());
                //库存处理
                myListItem.setQty(myListItem.getQty() + orderItem.getQty());
                myListItemService.update(myListItem);
            }
        }
    }
}
